package com.azzcs.blog.service;

import com.azzcs.blog.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author: wzg
 * @Date: 2021/1/16 下午12:55
 */
@Service
public class UserService {
    private List<UserEntity> userList = new ArrayList<>();

    @Autowired
    private RedisTemplate redisTemplate;

    public UserService() {
        this.userList.add(new UserEntity(1L,"zhangsan","zhangsan"));
        this.userList.add(new UserEntity(2L,"lisi","lisi"));
        this.userList.add(new UserEntity(3L,"wangwu","wangwu"));
        this.userList.add(new UserEntity(4L,"zhaoliu","zhaoliu"));
    }

    public boolean login(String sessionId, UserEntity userEntity) {

        for (UserEntity user : userList) {
            if(user.getUsername().equals(userEntity.getUsername())
                && user.getPassword().equals(userEntity.getPassword())){
                redisTemplate.opsForValue().set(sessionId,user.getId(),1, TimeUnit.MINUTES);
                return true;
            }
        }
        return false;
    }
}
